package com.xyazm.inventory.api;

import com.xyazm.common.core.JsonResult;
import com.xyazm.inventory.domain.param.ReleaseProductStockParam;
import com.xyazm.inventory.exception.InventoryBizException;
import com.xyazm.inventory.service.IInventoryService;
import com.xyazm.inventory.domain.param.DeductProductParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 库存服务对外暴露接口
 * @author xuyou
 * @date 2022年06月04日 22:30
 */
@Slf4j
@DubboService(version = "1.0.0",interfaceClass = IInventoryApi.class)
public class InventoryApi implements IInventoryApi {

    @Autowired
    private IInventoryService inventoryService;

    @Override
    public JsonResult<Void> deductProductStock(DeductProductParam param) {
        try {
            inventoryService.deductProductStock(param);
            return JsonResult.buildSuccess();
        } catch (InventoryBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult<Void> releaseProductStock(ReleaseProductStockParam param) {
        try {
            inventoryService.releaseProductStock(param);
            return JsonResult.buildSuccess();
        } catch (InventoryBizException e) {
            log.error("biz error", e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch(Exception e) {
            log.error("system error", e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
